import type { App } from "vue";
import { createRouter, createWebHashHistory, type RouteRecordRaw } from "vue-router";

export const Layout = () => import("@/layout/index.vue");

// 静态路由
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index.vue"),
      },
    ],
  },

  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: { hidden: true },
  },

  {
    path: "/",
    name: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/dashboard/index.vue"),
        name: "Dashboard",
        meta: {
          title: "首页",
          icon: "homepage",
          affix: true,
          keepAlive: true,
        },
      },

      {
        path: "401",
        component: () => import("@/views/error/401.vue"),
        meta: { hidden: true },
      },
      {
        path: "404",
        component: () => import("@/views/error/404.vue"),
        meta: { hidden: true },
      },
    ],
  },

  {
    path: "/dataView",
    name: "DataView",
    component: Layout,
    redirect: "/index",
    children: [
      {
        path: "index",
        component: () => import("@/views/data-view/index.vue"),
        name: "DataViewIndex",
        meta: {
          title: "数据看板",
          icon: "homepage",
          affix: true,
          keepAlive: true,
        },
      },
    ],
  },

  {
    path: "/system",
    name: "System",
    redirect: "/system/admin",
    component: Layout,
    meta: {
      title: "系统管理",
      icon: "system",
      alwaysShow: true,
    },
    children: [
      {
        path: "admin",
        component: () => import("@/views/system/admin/index.vue"),
        name: "Admin",
        meta: {
          title: "管理员",
          icon: "el-icon-User",
          affix: true,
        },
      },
      {
        path: "admin/add",
        component: () => import("@/views/system/admin-add/index.vue"),
        name: "AdminAdd",
        meta: {
          title: "新增管理员",
          hidden: true,
        },
      },
      {
        path: "admin/edit",
        component: () => import("@/views/system/admin-edit/index.vue"),
        name: "AdminEdit",
        meta: {
          title: "编辑管理员",
          hidden: true,
        },
      },

      {
        path: "role",
        component: () => import("@/views/system/role/index.vue"),
        name: "Role",
        meta: {
          title: "角色管理",
          icon: "role",
          affix: true,
        },
      },

      {
        path: "role/add",
        component: () => import("@/views/system/role-add/index.vue"),
        name: "RoleAdd",
        meta: {
          title: "新增角色",
          hidden: true,
        },
      },

      {
        path: "role/edit",
        component: () => import("@/views/system/role-edit/index.vue"),
        name: "RoleEdit",
        meta: {
          title: "编辑角色",
          hidden: true,
        },
      },
    ],
  },

  {
    path: "/location",
    name: "Location",
    component: Layout,
    meta: {
      title: "定位管理",
      icon: "cascader",
      alwaysShow: true,
    },
    children: [
      {
        path: "revamp",
        component: () => import("@/views/location/revamp/index.vue"),
        name: "LocationRevamp",
        meta: {
          title: "改造分布图",
          icon: "el-icon-memo",
          affix: true,
        },
      },
    ],
  },

  {
    path: "/user",
    name: "User",
    redirect: "/user/list",
    component: Layout,
    meta: {
      title: "客户管理",
      icon: "cascader",
      alwaysShow: true,
    },
    children: [
      {
        path: "list",
        name: "UserList",
        component: () => import("@/views/user/list/index.vue"),
        meta: {
          title: "客户列表",
          icon: "el-icon-memo",
          affix: true,
        },
      },

      {
        path: "detail",
        name: "UserDetail",
        component: () => import("@/views/user/detail/index.vue"),
        meta: {
          title: "客户详情",
          affix: true,
          hidden: true,
        },
      },

      {
        path: "add",
        name: "UserAdd",
        component: () => import("@/views/user/add/index.vue"),
        meta: {
          title: "客户新增",
          hidden: true,
        },
      },

      {
        path: "edit",
        name: "UserEdit",
        component: () => import("@/views/user/edit/index.vue"),
        meta: {
          title: "客户编辑",
          hidden: true,
        },
      },
    ],
  },

  {
    path: "/revamp",
    name: "Revamp",
    redirect: "/revamp/list",
    component: Layout,
    meta: {
      title: "改造管理",
      icon: "cascader",
      alwaysShow: true,
    },
    children: [
      {
        path: "list",
        name: "RevampList",
        component: () => import("@/views/revamp/list/index.vue"),
        meta: {
          title: "改造情况列表",
          icon: "el-icon-memo",
          affix: true,
        },
      },

      {
        path: "roster",
        name: "RevampRoster",
        component: () => import("@/views/revamp/roster/index.vue"),
        meta: {
          title: "改造花名册",
          affix: true,
        },
      },
    ],
  },

  {
    path: "/device",
    name: "Device",
    redirect: "/device/list",
    component: Layout,
    meta: {
      title: "设备管理",
      icon: "cascader",
      alwaysShow: true,
    },
    children: [
      {
        path: "list",
        name: "DeviceList",
        component: () => import("@/views/device/list/index.vue"),
        meta: {
          title: "设备列表",
          icon: "el-icon-memo",
          affix: true,
        },
      },

      {
        path: "form",
        name: "DeviceForm",
        component: () => import("@/views/device/form/index.vue"),
        meta: {
          title: "设备",
          hidden: true,
        },
      },
      {
        path: "situation",
        name: "DeviceSituation",
        component: () => import("@/views/device/situation/index.vue"),
        meta: {
          title: "设备情况",
          icon: "el-icon-memo",
          affix: true,
        },
      },

      {
        path: "situation/detail",
        name: "DeviceSituationDetail",
        component: () => import("@/views/device/situation-detail/index.vue"),
        meta: {
          title: "设备情况详情",
          hidden: true,
        },
      },
    ],
  },

  {
    path: "/house",
    name: "house",
    redirect: "/house/list",
    component: Layout,
    meta: {
      title: "户型管理",
      icon: "cascader",
      alwaysShow: true,
    },
    children: [
      {
        path: "list",
        name: "HouseList",
        component: () => import("@/views/house/list/index.vue"),
        meta: {
          title: "户型列表",
          icon: "el-icon-memo",
          affix: true,
        },
      },

      {
        path: "form",
        name: "HouseForm",
        component: () => import("@/views/house/form/index.vue"),
        meta: {
          title: "户型",
          hidden: true,
        },
      },
    ],
  },
];

/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

// 全局注册 router
export function setupRouter(app: App<Element>) {
  app.use(router);
}

export default router;
